﻿{include file="public/header"/}
</head>
<body>
<style>
@media screen and (max-width:500px){
.layui-layer.layui-layer-page{width:100% !important;overflow:hidden !important;left:0 !important;}
.layui-layer-title{width:100% !important;box-sizing:border-box;}
.layui-layer-content{width:100% !important;}
}
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                    <button id="btnAddUser" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>新增广告</button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>
<script type="text/html" id="tableBarUser">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="pcftag">标签调用</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
{include file="public/footer"/}
<script>
    layui.use(['layer', 'form', 'table'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: "{:url('/channel.adposition/index')}",
            page: true,
            cellMinWidth: 100,
            cols: [[
                {field:'id',title:'ID',width:60},
                {field: 'title', sort: true, title: '广告名称'},
                {align: 'center', toolbar: '#tableBarUser', title: '操作', width: 200}
            ]]
        });
        // 添加
        $('#btnAddUser').click(function () {
            showEditModel();
        });
        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                showEditModel(data);
            } else if (layEvent === 'del') {
                doDel(data.id, data.title);
            } else if (layEvent === 'pcftag') {
                resetPsw(data.id, 1);
            }
        });
        // 显示表单弹窗
        function showEditModel(mUser) {
            if(mUser){
                var id = mUser.id;
                $.get('{:url("/channel.adposition/edit")}?id='+id+'&ajax=1', function(tpl){
                    if(tpl.status){
                        var iframes = layer.open({
                          type: 2,
                          title: '编辑广告',
                          fixed: true, //不固定
                          shadeClose: false,
                          shade: 0.3,
                          content: '{:url("/channel.adposition/edit")}?id='+id
                        });
                        layer.full(iframes); 
                    }else{
                        layer.msg(tpl.msg, {icon: 2,time:1000});
                    }
                }, 'json');
            }
            else{
                $.get('{:url("/channel.adposition/add")}?ajax=1', function(tpl){
                    if(tpl.status){
                        var iframes = layer.open({
                          type: 2,
                          title: '新增广告',
                          fixed: true, //不固定
                          shadeClose: false,
                          shade: 0.3,
                          content: '{:url("/channel.adposition/add")}'
                        });
                        layer.full(iframes);
                    }else{
                        layer.msg(tpl.msg, {icon: 2,time:1000});
                    }
                }, 'json');
            }
        }

        //删除
        function doDel(id, ntitle) {
            layer.msg('确定要删除“' + ntitle + '”吗？', {
                btnAlign: 'c',
                time: 0,
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    layer.close(index);
                    $.get("{:url('/channel.adposition/del')}", {
                        id: id
                    }, function (res) {
                        layer.closeAll('loading');
                        if (res.status) {
                            layer.msg(res.msg, {icon: 1,time:1500});
                            insTb.reload({}, 'data');
                        } else {
                            layer.msg(res.msg, {icon: 2,time:1500});
                        }
                    }, 'json');
                }
            });
        }

        function resetPsw(id, limit) {
            var advstr = "{gzpcf:adv pid='" + id + "'}\r\n<img src='{$"+"field.litpic}' alt='{$"+"field.title}' />\r\n{/gzpcf:adv";
            var contentdiv = '<div class="dialog_content" style="margin:0px;padding:0px;"><dl style="padding:10px 30px;line-height:30px"><dd>标签 adv 调用：</dd>'
            contentdiv += '<textarea rows="4" cols="60" style="width:100%;min-height:100px;">' + advstr + '}</textarea>'
            contentdiv += '<dd style="border-top:dotted 1px #E7E7E7;color:#F60;">请将标签adv代码复制并粘贴到对应模板文件中！</dd></dl></div>'
            layer.open({
                title: '代码调用',
                type: 1,
                skin: 'layui-layer-demo',
                area: ['480px', '280px'],
                content: contentdiv
            });
        }
    });
</script>
</body>
</html>